var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }

    let customerData = [];
    let salesmanList=[];
    layer.ready(function () {
        //公司名称
        let customerType=getdictionary(6,27);
        let customerTypeList = [];
        for (let i in customerType) {
            let tmp = {
                name:customerType[i].name,
                value:customerType[i].code
            }
            customerTypeList.push(tmp);
        }
        customerData = getxmselect("#customerType", "customerType", customerTypeList);
        form.render('select');

        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();
        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", userTree);
        } else {
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", [{name: user.name, value: user.code}])
        }

    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#customerInformationRecordSelect',
        data:initData,
        toolbar: '#customerInformationRecordToolbar',
        cols: [[
            {field: 'numbers',type:'numbers',title:'序号'},
            {field: 'code', title: '客户编码', width: 200, align: 'center'},
            {field: 'name', title: '客户名称', width: 400, align: 'center'},
            {field: 'abbreviation', title: '简称', width: 200, align: 'center'},
            {field: 'typeName', title: '客户类型', width: 300, align: 'center'},
            {field: 'salesmanName', title:'业务员', width: 150, align: 'center'},
            {field: 'address', title:'客户地址', width: 400, align: 'center'},
            {field: 'levelName', title: '等级', sort: true, width: 150, align: 'center'},
            {field: 'accountPeriod', title:'账期', sort: true, width: 100, align: 'center'},
            {field: 'creditLine', title:'信用额度', sort: true, width: 100, align: 'center'},
            {field: 'country', title:'国家', width: 180, align: 'center'},
            {field: 'sourceOfCustomers', title:'客户来源', width: 200, align: 'center'},
            {field: 'attributesOfCustomers', title: '客户属性' ,width: 200},
            {field: 'shortCode', title:'三位简码', width: 200, align: 'center'},
            {field: 'isMaker', title:'是否下单', width: 200, align: 'center'},
            {field: 'updateTimeDateStr', title:'更改日期', width: 200, align: 'center'},
            {field: 'createTimeDateStr', title:'创建日期', width: 200, align: 'center'}
        ]],
        id: 'maininformationtableid',
        height: $(document).height() - $('#customerInformationRecordSelect').offset().top - 75,
        //开启分页
        page: true,
        limits: [300, 600, 900]
    });


    //搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'customer/customerInfoListByPage',
            where:  data.field
        }, 'data');
        return false;
    });
    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"customer/customerInfoListExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'X': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '客户信息记录.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

    document.getElementById('clear').onclick = function(){
        customerData.setValue([ ]);
        salesmanList.setValue([ ]);
    };
});

